/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.Constants;
import blueprint4j.utils.*;

import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;

public class DataPairFieldStringPassword extends DataPairDefault implements FieldBinding,FocusListener {
    private BindPasswordInterface field;
    private JPasswordField text_field=null;
    private JPasswordField text_field2=null;
    private Keyboard keyboard = null;
    
    public DataPairFieldStringPassword(Binder binder,BindPasswordInterface p_field){
        super(binder,p_field);
        text_field=new JPasswordField(10);
        text_field2=new JPasswordField(10);
        if (Keyboard.GUI_VIRTUAL_KEYBOARD) {
            text_field.addFocusListener(this);
            text_field2.addFocusListener(this);
        }
        field=p_field;
        setLabel(new JLabel(field.getDescription()));
        Box box=Box.createVerticalBox();
        box.add(text_field);
        if (field.getVerifyPassword()) {
            box.add(text_field2);
        }
        setComponent(box);
    }
    
    public boolean isChanged(){
        String f=field.get();
        if (!field.getVerifyPassword()) {
            text_field2.setText(new String(text_field.getPassword()));
        }
        if (text_field.getPassword()==null ^ text_field2.getPassword()==null){
            return true;
        }
        if (text_field.getPassword()!=null){
            if (!new String(text_field.getPassword()).equals(new String(text_field2.getPassword()))){
                return true;
            }
        }
        if (f==null){
            return text_field.getPassword() != null;
        } else {
            return !f.equals(new String(text_field.getPassword()));
        }
    }
    
    public void focusGained(FocusEvent e) {
        if (keyboard == null) {
            if (Gui.getParentFrame(((JPasswordField)e.getSource()))!= null) {
                keyboard = new Keyboard(Gui.getParentFrame(((JPasswordField)e.getSource())),field.getDescription(),true);
            } else if (Gui.getParentDialog(text_field)!= null) {
                keyboard = new Keyboard(Gui.getParentDialog(((JPasswordField)e.getSource())),field.getDescription(),true);
            } else {
                keyboard = new Keyboard();
            }
            keyboard.setText(new String(((JPasswordField)e.getSource()).getPassword()));
            keyboard.setVisible(true);
            if (keyboard.getText() != null && keyboard.getText().length() > 0) {
                ((JPasswordField)e.getSource()).setText(keyboard.getText());
            }
        } else {
            keyboard = null;
        }
    }
    
    public void focusLost(FocusEvent e) {}
    
    public void intoControl(){
        text_field.setText(field.get());
        text_field2.setText(field.get());
    }
    
    public void intoBindable() {
        try {
            if (!field.getVerifyPassword()) {
                text_field2.setText(new String(text_field.getPassword()));
            }
            if (text_field.getPassword()!=null){
                if (!new String(text_field.getPassword()).equals(new String(text_field2.getPassword()))){
                    throw new BindException("The password fields for "+field.getDescription()+" must match");
                }
            } else {
                if (text_field2.getPassword()==null){
                    throw new BindException("The password fields for "+field.getDescription()+" must match");
                }
            }
            field.set(new String(text_field.getPassword()));
        } catch (Exception exception) {
            Log.debug.out(exception);
        }
    }
    
    public Bindable getBindable(){
        return field.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindPasswordInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
        text_field2.setText("");
    }
}

